package net.maxwoods.springcloud;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class EurekaClientController {

    @Autowired
    private Registration registration;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    ClientUserService clientUserService;

    @Autowired
    FeignClientTest feignClientTest;

    @Autowired
    private TestConfig testConfig;

    @RequestMapping("/client")
    public String clinet() {
        List<String> serviceIds = discoveryClient.getServices();
        StringBuilder sb = new StringBuilder();
        for (String serviceId : serviceIds
        ) {
            sb.append(serviceId);
            sb.append("\n");
        }
        sb.append(testConfig.getRedisServer());
        sb.append("\n");
        sb.append(testConfig.getTest());
        sb.append("\n");
        return sb.toString();
    }

    @RequestMapping("/discovery")
    public String discovery() {
        String serviceId = registration.getServiceId();
        List<ServiceInstance> serviceInstances = discoveryClient.getInstances("USER-SERVICE");
        StringBuilder sb = new StringBuilder();
        for (ServiceInstance serviceInstance : serviceInstances) {
            sb.append(serviceInstance.getServiceId());
            sb.append("\n");
        }
        return sb.toString();
    }

    @RequestMapping("/consume")
    public String consume() {
        return clientUserService.test();
    }

    @RequestMapping("/feign")
    public String feign() {
        return feignClientTest.getTest();
    }
}
